<template>
  <el-tabs v-model="activeName" @tab-click="handleClick" stretch="true" style="margin-top: 40px">
    <el-tab-pane label="药品总库存" name="first"  style="height: 800px ;width: 1700px ; margin-left: 40px">
      <el-form :model="drugForm" ref="queryForm" v-show="showSearch" :inline="true" style="margin-top: 20px">
        <el-form-item label="药品名称" prop="drugName" style="margin-left: 40px">
          <el-input
            v-model="drugForm.drugName"
            placeholder="药品名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="药品类型" prop="drugType">
          <el-select
            v-model="drugForm.drugType"
            placeholder="药品类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in drugTypes"
              :key="dict.drugType"
              :label="dict.drugType"
              :value="dict.drugType"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂家" prop="facId">
          <el-select
            v-model="drugForm.facId"
            placeholder="生产厂家"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in factoryList"
              :key="dict.facId"
              :label="dict.facName"
              :value="dict.facId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="处方类型" prop="drugRxType">
          <el-select
            v-model="drugForm.drugRxType"
            placeholder="单据状态"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in drugTypes"
              :key="dict.drugRxType"
              :label="dict.drugRxType"
              :value="dict.drugRxType"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <el-table  :data="drugAll"  style="margin-top: 30px">
        <el-table-column label="药品ID" align="center" prop="drugId" style="width: 40px" />
        <el-table-column label="药品名称" align="center" prop="drugName"  />
        <el-table-column label="库存量" align="center" prop="drugStocks"  />
        <el-table-column label="预警值" align="center" prop="drugWarning"  />
        <el-table-column label="生产厂家" align="center" prop="factoryName"  />
        <el-table-column label="药品类型" align="center" prop="drugType"  />
        <el-table-column label="处方类型" align="center" prop="drugRxType"  />
        <el-table-column label="单位" align="center" prop="drugUnit"  />
        <el-table-column label="换算量" align="center" prop="drugMatrixing"  />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="drugForm.pageNum"
        :limit.sync="drugForm.pageSize"
        @pagination="getList"
      />
    </el-tab-pane>
    <el-tab-pane label="批次库存及价格" name="second" >
      <el-form :model="drugForm" ref="queryForm" v-show="showSearch" :inline="true" style="margin-top: 20px">
        <el-form-item label="采购单据号" prop="drugName" style="margin-left: 40px">
          <el-input
            v-model="drugForm.drugName"
            placeholder="采购单据号"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="药品名称" prop="drugName" >
          <el-input
            v-model="drugForm.drugName"
            placeholder="药品名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="药品类型" prop="drugType">
          <el-select
            v-model="drugForm.drugType"
            placeholder="药品类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in drugTypes"
              :key="dict.drugType"
              :label="dict.drugType"
              :value="dict.drugType"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂家" prop="facId">
          <el-select
            v-model="drugForm.facId"
            placeholder="生产厂家"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in factoryList"
              :key="dict.facId"
              :label="dict.facName"
              :value="dict.facId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="处方类型" prop="drugRxType">
          <el-select
            v-model="drugForm.drugRxType"
            placeholder="单据状态"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in drugTypes"
              :key="dict.drugRxType"
              :label="dict.drugRxType"
              :value="dict.drugRxType"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" style="margin-left: 40px">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery2">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table  :data="drugPurFacAll"  style="margin-top: 30px ; width: 1700px ; margin-left: 40px"  >
        <el-table-column label="药品ID" align="center" prop="drugId"  width="80px" />
        <el-table-column label="药品名称" align="center" prop="drugName"  />
        <el-table-column label="采购量" align="center" prop="purNumbers"  />
        <el-table-column label="批发价" align="center" prop="purPrice"  />
        <el-table-column label="批发额" align="center" prop="purCount"   />
        <el-table-column label="批次号" align="center" prop="purId" />
        <el-table-column label="生产厂家" align="center" prop="factoryName" width="200px" />
        <el-table-column label="药品类型" align="center" prop="drugType"  />
        <el-table-column label="处方类型" align="center" prop="drugRxType"  />
        <el-table-column label="创建时间" align="center" prop="createTime"  />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="drugForm.pageNum"
        :limit.sync="drugForm.pageSize"
        @pagination="getList"
      />
    </el-tab-pane>



  </el-tabs>
</template>

<script>
//import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/system/role";
import { getFactory , getPur , getDrug , getDrugTypes ,  getAllDrug , listDrug , listPurDrug , findAllPurDrugFac} from "@/api/purchase/stockinquiry";
import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";

export default {
  name: "Purchase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      purOrderIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 药品类型及处方类型列表
      drugTypes:[],
      //厂家列表
      factoryList: [],
      //所有药品列表：
      drugAll:[],
      //订单整理列表
      drugPurFacAll:[],
      // 数据范围选项
      dataScopeOptions: [
        {
          value: "1",
          label: "全部数据权限"
        },
        {
          value: "2",
          label: "自定数据权限"
        },
        {
          value: "3",
          label: "本部门数据权限"
        },
        {
          value: "4",
          label: "本部门及以下数据权限"
        },
        {
          value: "5",
          label: "仅本人数据权限"
        }
      ],
      // 菜单列表
      menuOptions: [],

      // 查询参数


      drugForm:{
        pageNum: 1,
        pageSize: 10,
        purOrderIds:null,
        drugKey:null,
        drugType:null,
        drugRxType:null,
        facId:null
      },
      // 表单参数
      form: {},

      // 表单校验
      depts: {
        DeptsName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "权限字符不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getpur();
    this.getDrugType()
    this.getFacList();

    this.getList();


    this.getDicts("purchase_status").then(response => {
      this.purchaseStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询检查项目列表 */
    getList() {
      this.loading = true;
      listDrug(this.addDateRange(this.drugForm, this.dateRange)).then(
        response => {
          this.drugAll = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    getpur() {
      this.loading = true;
      alert(1)
      findAllPurDrugFac(this.addDateRange(this.drugForm, this.dateRange)).then(
        response => {
          this.drugPurFacAll = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getFacList() {
      this.loading = true;
      getFactory().then(
        response => {
          this.factoryList = response.data;
        }
      );
    },
    getDrugType(){
      getDrugTypes().then(respones=>{
        this.drugTypes = respones.data;
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
      this.menuNodeAll = false,
      this.deptExpand = true,
      this.deptNodeAll = false,
      this.form = {
        examId: undefined,
        examName: undefined,
        examKey: undefined,
        examOnePrice: undefined,
        examCost: undefined,
        examUnit: undefined,
        status: "0",
        deptIds: [],
        menuCheckStrictly: true,
        deptCheckStrictly: true,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 药品总库搜索按钮操作 */
    handleQuery() {
      this.drugForm.pageNum = 1;
      this.getList();
    },
    /** 批次搜索按钮操作 */
    handleQuery2() {
      this.drugForm.pageNum = 1;
      this.getpur();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.purChases=selection.map(item => item)
      for (var i = 0 ; i < this.purChases.length ; i++){
        if (this.purOrderIds.indexOf(this.purChases[i].purOrderId)==-1) {
          this.purOrderIds.push(this.purChases[i].purOrderId)
        }
      }

      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ name:'Detail'})
    },

    /** 提交审核按钮操作 */
    handleUpdate(row) {
      const purOrderIds = this.purOrderIds
      putCheck(purOrderIds).then(response => {
        this.getList();
      });
    },


    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.examId != undefined) {
            updateExam_cost(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExam_cost(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除作废按钮操作 */
    handleDelete(row) {
      const purOrderIds = this.purOrderIds
      deletePur(purOrderIds).then(response => {
        this.getList();
      });
    },

    RuChangeStatus(row) {
      var b = confirm("是否提交" + this.purOrderIds + "订单入库")
      if (b) {
        Ruchange(this.purOrderIds).then(response => {
          this.getList();
        });
      }

    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },

    // 菜单状态字典翻译
    purVerifyFormat(row, column) {
      return this.selectDictLabel(this.purchaseStatusOptions, row.purVerify);
    }

    },



};
</script>
